package com.shop.prj.dao.Impl;

import com.shop.prj.bean.BookStyle;
import com.shop.prj.bean.Feedback;
import com.shop.prj.bean.Order;
import com.shop.prj.dao.OrderDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int insert(Order order) {
        String sql="INSERT INTO `order`(`user_id`,`status`) VALUES (?,?)";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);

            ;
            preparedStatement.setInt(1,order.getUserId());
            preparedStatement.setInt(2,order.getStatus());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

    @Override
    public int update(Order order) {
        String sql="UPDATE `order` SET `status`=? WHERE `order_id`=?";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
       
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,order.getStatus());
            preparedStatement.setInt(2, order.getOrderId());

            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }



    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM `order` WHERE `order_id`=?";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }



    @Override
    public List<Order> queryAll() {
        String sql = "SELECT * FROM `order`";

        Connection connection = JDBCUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        List<Order> list = new ArrayList<Order>();
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                Order order=new Order();
                order.setOrderId(resultSet.getInt(1));
//                order.setOrderItemId(resultSet.getInt(2));
                order.setUserId(resultSet.getInt(2));
                order.setStatus(resultSet.getInt(3));

                list.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

	@Override
	public Order queryByOrderId(Integer id) {
		 String sql = "SELECT * FROM `order` WHERE `order_id`=?";

	        Connection connection = JDBCUtils.getConnection();
	        PreparedStatement preparedStatement = null;

	        ResultSet resultSet = null;
	        BookStyle  bookStyle = null;
	        Order order=null;
	        try {
	            preparedStatement = connection.prepareStatement(sql);
	            preparedStatement.setInt(1, id);
	            resultSet = preparedStatement.executeQuery();
	            if (resultSet.next()) {
	            	order=new Order();
	                order.setOrderId(resultSet.getInt(1));
//	                order.setOrderItemId(resultSet.getInt(2));
	                order.setUserId(resultSet.getInt(2));
	                order.setStatus(resultSet.getInt(3));

	               

	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                resultSet.close();
	                preparedStatement.close();
	                connection.close();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }

	        }


	        return order;
	}


}
